from fastapi import APIRouter, HTTPException
from ..services.ocr import OCRService
from ..models.request import OCRResponse
from ..utils.logger import setup_logger
import os

router = APIRouter()
logger = setup_logger('ocr_api')

@router.post("/ocr/{filename}", response_model=OCRResponse)
async def process_ocr(filename: str):
    """OCR处理接口"""
    try:
        # 构建文件路径
        file_path = os.path.join("upload/extracted", filename)
        if not os.path.exists(file_path):
            raise HTTPException(status_code=404, detail="文件不存在")
        
        # 处理PDF文件
        text, image_paths = OCRService.process_pdf(file_path)
        
        # 返回结果
        return OCRResponse(
            status="success",
            message="OCR处理完成",
            text=text,
            image_paths=[os.path.basename(p) for p in image_paths]
        )
        
    except Exception as e:
        logger.error(f"OCR处理失败: {str(e)}")
        raise HTTPException(status_code=500, detail=str(e)) 